package com.dapangdong.testdemo.utils;

import cn.hutool.core.io.FileTypeUtil;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

/**
 * <p>
 *
 * </p>
 *
 * @author : yhd
 * @program: gulimall
 * @since : 2023-05-26 10:33
 */
public class FileTypeUtils {
    private final static String IMAGE_TYPE = "image/";
    private final static String AUDIO_TYPE = "audio/";
    private final static String VIDEO_TYPE = "video/";
    private final static String APPLICATION_TYPE = "application/";
    private final static String TXT_TYPE = "text/";

    public static String getFileType(MultipartFile multipartFile) {
        InputStream inputStream = null;
        String type = null;
        try {
            inputStream = multipartFile.getInputStream();
            type = FileTypeUtil.getType(inputStream);
            System.out.println(type);
            if ("JPG".equalsIgnoreCase(type) || "JPEG".equalsIgnoreCase(type) || "GIF".equalsIgnoreCase(type) || "PNG".equalsIgnoreCase(type) || "BMP".equalsIgnoreCase(type) || "PCX".equalsIgnoreCase(type) || "TGA".equalsIgnoreCase(type) || "PSD".equalsIgnoreCase(type) || "TIFF".equalsIgnoreCase(type)) {
                return IMAGE_TYPE + type;
            }
            if ("mp3".equalsIgnoreCase(type) || "OGG".equalsIgnoreCase(type) || "WAV".equalsIgnoreCase(type) || "REAL".equalsIgnoreCase(type) || type.equalsIgnoreCase("APE") || type.equalsIgnoreCase("MODULE") || type.equalsIgnoreCase("MIDI") || type.equalsIgnoreCase("VQF") || type.equalsIgnoreCase("CD")) {
                return AUDIO_TYPE + type;
            }
            if (type.equalsIgnoreCase("mp4") || type.equalsIgnoreCase("avi") || type.equalsIgnoreCase("MPEG-1") || type.equalsIgnoreCase("RM") || type.equalsIgnoreCase("ASF") || type.equalsIgnoreCase("WMV") || type.equalsIgnoreCase("qlv") || type.equalsIgnoreCase("MPEG-2") || type.equalsIgnoreCase("MPEG4") || type.equalsIgnoreCase("mov") || type.equalsIgnoreCase("3gp")) {
                return VIDEO_TYPE + type;
            }
            if (type.equalsIgnoreCase("doc") || type.equalsIgnoreCase("docx") || type.equalsIgnoreCase("ppt") || type.equalsIgnoreCase("pptx") || type.equalsIgnoreCase("xls") || type.equalsIgnoreCase("xlsx") || type.equalsIgnoreCase("zip") || type.equalsIgnoreCase("jar")) {
                return APPLICATION_TYPE + type;
            }
            if (type.equalsIgnoreCase("txt")) {
                return TXT_TYPE + type;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
